package com.sy.workflow.service.exceptionHandler;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.sy.core.bean.CommonResult;
import com.sy.core.exceptions.BusinessException;

import lombok.extern.slf4j.Slf4j;

@RestControllerAdvice
@Slf4j
@Component("EdcGlobalExceptionHandle")
public class GlobalExceptionHandle {


    @ExceptionHandler(value = Exception.class)
    public CommonResult<Object> handle(Exception e) {
        if (e instanceof BusinessException) {
            log.error("GlobalException" + e.getMessage());
            return CommonResult.failed(((BusinessException) e).getCode(), e.getMessage());
        } else if (e instanceof MissingServletRequestParameterException) {
            log.error("MissingServletRequestParameter" + e.getMessage());
            return CommonResult.failed("参数校验失败");
        } else if (e instanceof HttpRequestMethodNotSupportedException) {
            log.error("HttpRequestMethodNotSupportedException" + e.getMessage());
            return CommonResult.failed("参数校验失败");
        } else if (e instanceof HttpMessageNotReadableException) {
            log.error("HttpMessageNotReadableException" + e.getMessage());
            return CommonResult.failed("请检查是否传入参数");
        } else {
            log.error("系统异常", e);
            return CommonResult.failed("系统执行错误");
        }
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public CommonResult<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        
    	List<ObjectError> objectErrors = e.getBindingResult().getAllErrors();
        
        if (!CollectionUtils.isEmpty(objectErrors)) {
            String errorMessage = objectErrors.stream().map(r -> r.getDefaultMessage()).collect(Collectors.joining(","));
            log.info("参数校验失败{}", errorMessage);
            return CommonResult.failed(errorMessage);
        } else {
            log.info("参数校验失败");
            return CommonResult.failed("参数校验失败");
        }
        
    }

    @ExceptionHandler(BindException.class)
    public CommonResult<Object> handleBindException(BindException e) {
    	
        List<ObjectError> objectErrors = e.getAllErrors();
        
        if (!CollectionUtils.isEmpty(objectErrors)) {
            String errorMessage = objectErrors.stream().map(r -> r.getDefaultMessage()).collect(Collectors.joining(","));
            log.info("绑定参数失败{}", errorMessage);
            return CommonResult.failed(errorMessage);
        } else {
            log.info("绑定参数失败");
            return CommonResult.failed("参数校验失败");
        }
        
    }
}
